package com.windea.study.kotlin.game.tankwar2.core

import javafx.animation.*
import javafx.scene.paint.*

class Looper(
	private val window: Window
) : AnimationTimer() {
	val width = window.width.toDouble()
	val height = window.height.toDouble()
	private var lastTime = 0L
	
	override fun handle(now: Long) {
		if(lastTime == 0L) {
			lastTime = now
		} else {
//        println(">>>>>>>>>>> ${(now - lastTime) / 1000 / 1000} <<<<<<<<<<<<<<")
//        window.sleep = (now - lastTime) / 1000 / 1000
			window.fps = 1000 * 1000 * 1000 / (now - lastTime)
		}
		lastTime = now
		
		val graphicsContext = window.canvas.graphicsContext2D
		//清屏
		//gc.clearRect(0.0, 0.0, width, height)
		
		graphicsContext.fill = Color.BLACK
		graphicsContext.fillRect(0.0, 0.0, width, height)
		
		window.onDisplay()
		System.gc()
	}
}
